package com.lc.user.express.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.model.UsedRouteModel;
import com.lc.user.express.ordering.UsedRouteDetailsActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/3/7.
 */
public class UsedRouteAdapter extends BaseListAdapter<UsedRouteModel> {
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;

    public UsedRouteAdapter(Context context, List<UsedRouteModel> data) {
        super(context, data);
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).isEnd()) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        int type = getItemViewType(position);
        UsedRouteModel model = data.get(position);
       if (convertView == null) {
            if (type == TYPE_1) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.footer_select_used_addr_list, null);
                AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
                viewHolder1 = new ViewHolder1();
                viewHolder1.tv_title = (TextView)convertView.findViewById(R.id.tv_dic);
                convertView.setTag(viewHolder1);
            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_used_route_list, null);
                AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
                viewHolder = new ViewHolder();
                viewHolder.tv_start_addr = (TextView) convertView.findViewById(R.id.tv_start_addr);
                viewHolder.tv_selected = (TextView) convertView.findViewById(R.id.tv_selected);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.tv_end_addr_count = (TextView) convertView.findViewById(R.id.tv_end_addr_count);
                viewHolder.tv_look = (TextView) convertView.findViewById(R.id.tv_look);
                convertView.setTag(viewHolder);
            }


        }
        setContentView(convertView, model, type);

        return convertView;
    }

    private void setContentView(View contentView, final UsedRouteModel model, int type) {
        if (type == TYPE_1) {
            ViewHolder1 viewHolder = (ViewHolder1) contentView.getTag();
            viewHolder.tv_title.setText(model.getName());

        } else {
            ViewHolder viewHolder = (ViewHolder) contentView.getTag();
            viewHolder.tv_name.setText(model.getName());
            viewHolder.tv_end_addr_count.setText(model.getEndAddrCount()-1 + "个目的地");
            viewHolder.tv_start_addr.setText("始发地：" + model.getStartAddr());
            viewHolder.tv_selected.setBackgroundColor(model.isSelect() ? mContext.getResources().getColor(R.color.green) : mContext.getResources().getColor(R.color.gray1));

            viewHolder.tv_look.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(mContext, UsedRouteDetailsActivity.class);
                    i.putExtra("title", model.getName());
                    i.putExtra("routeId", model.getRouteId());
                    mContext.startActivity(i);
                }
            });
        }

    }

    private class ViewHolder {
        TextView tv_start_addr;
        TextView tv_selected;
        TextView tv_name;
        TextView tv_end_addr_count;
        TextView tv_look;
    }

    final static class ViewHolder1 {
        TextView tv_title;
    }
}
